/**
 * Created by Freeman on 2016/8/12.
 */
import React, {Component, PropTypes} from 'react'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import * as Actions from '../../actions'
import Main from './Main'
import Preview from './Preview'
import './css/index.css'
class Writer extends Component {
    constructor(props) {
        super(props);
        this.handleChangeTitle = this.handleChangeTitle.bind(this);
        this.handleChangeContent = this.handleChangeContent.bind(this);
        this.handleSave = this.handleSave.bind(this);
        this.state = {
            title:'',
            content:''
        }

    }

    handleChangeTitle(event){
        this.setState({
            title:event.target.value,
            content:this.state.content
        })
    }
    handleChangeContent(event){
        this.setState({
            title:this.state.title,
            content:event.target.value
        })
    }

    handleSave(){
        console.log(this.state)
        const {publishArticle} = this.props.actions;
        publishArticle(this.state)
    }

    render() {
        return (
            <div className="container-fluid main-box preview-mode">
                <div className="row">
                    <Main title={this.state.title} content={this.state.content} changeTitle={this.handleChangeTitle} changeContent={this.handleChangeContent} publishArticle={this.handleSave}></Main>
                    <Preview title={this.state.title} content={this.state.content}></Preview>
                </div>
            </div>
        )
    }
}
Writer.propTypes = {
    actions:PropTypes.object.isRequired
};

const mapStateToProps = state => {
    return {
    }
};

const mapDispatchToProps = dispatch => {
    return {
        actions: bindActionCreators(Actions, dispatch)
    }
};

Writer = connect(mapStateToProps, mapDispatchToProps)(Writer);

export default Writer